﻿using System;
using System.Collections.Generic;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Xwltz.Framework.Framework;
using Xwltz.Framework.Helper;

namespace Xwltz.Framework.Cloud
{
    /// <summary>
    /// 阿里大于短信服务接口
    /// </summary>
    public class SmsService
    {
        /// <summary>
        /// 短信发送接口
        /// </summary>
        /// <returns></returns>
        public static string SendSms(string phone, string param)
        {
            if (param.IsNullOrEmpty())
            {
                param = "{\"code\":\"" + CommonBase.RndNum(6) + "\"}";
            }
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", FrameworkConfigs.AccessKeyId, FrameworkConfigs.AccessKeySecret);
            DefaultProfile.AddEndpoint("cn-hangzhou", "cn-hangzhou", FrameworkConfigs.Product, FrameworkConfigs.Domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);

            var request = new CommonRequest();
            CommonResponse sendSmsResponse;
            try
            {
                request.Method = MethodType.POST;
                request.Domain = "dysmsapi.aliyuncs.com";
                request.Version = "2017-05-25";
                request.Action = "SendSms";
                //request.Protocol = ProtocolType.HTTP;

                //必填:待发送手机号。支持以逗号分隔的形式进行批量调用，批量上限为20个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
                request.AddQueryParameters("PhoneNumbers", phone);
                //必填:短信签名-可在短信控制台中找到
                request.AddQueryParameters("SignName", FrameworkConfigs.SignName);
                //必填:短信模板-可在短信控制台中找到
                request.AddQueryParameters("TemplateCode", FrameworkConfigs.TemplateCode);
                //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
                request.AddQueryParameters("TemplateParam", param);
                //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
                request.AddQueryParameters("OutId", "login sms");
                //请求失败这里会抛ClientException异常
                sendSmsResponse = acsClient.GetCommonResponse(request);
            }
            catch (ServerException ex)
            {
                Console.WriteLine(ex);
                throw;
            }
            catch (ClientException ex)
            {
                Console.WriteLine(ex);
                throw;
            }
            return System.Text.Encoding.UTF8.GetString(sendSmsResponse.HttpResponse.Content);
        }

        /// <summary>
        /// 短信查询接口
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="bizId"></param>
        /// <returns></returns>
        public static List<dynamic> QuerySendDetails(string phone, string bizId = "")
        {
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", FrameworkConfigs.AccessKeyId, FrameworkConfigs.AccessKeySecret);
            DefaultProfile.AddEndpoint("cn-hangzhou", "cn-hangzhou", FrameworkConfigs.Product, FrameworkConfigs.Domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);

            var request = new CommonRequest();
            CommonResponse sendSmsResponse;
            try
            {
                request.Method = MethodType.POST;
                request.Domain = "dysmsapi.aliyuncs.com";
                request.Version = "2017-05-25";
                request.Action = "QuerySendDetails";
                //request.Protocol = ProtocolType.HTTP;

                //必填:接收短信的手机号码。格式：国内短信：11位手机号码，例如15900000000。国际 / 港澳台消息：国际区号 + 号码，例如85200000000。
                request.AddQueryParameters("PhoneNumber", phone);
                //必填:短信发送日期，支持查询最近30天的记录。格式为yyyyMMdd，例如20181225。
                request.AddQueryParameters("SendDate", DateTime.Now.ToString("yyyyMMdd"));
                //必填:分页查看发送记录，指定每页显示的短信记录数量。取值范围为1~50。
                request.AddQueryParameters("PageSize", "50");
                //必填:分页查看发送记录，指定发送记录的的当前页码。
                request.AddQueryParameters("CurrentPage", "1");
                //发送回执ID，即发送流水号。调用发送接口SendSms或SendBatchSms发送短信时，返回值中的BizId字段。
                request.AddQueryParameters("BizId", bizId);

                //请求失败这里会抛ClientException异常
                sendSmsResponse = acsClient.GetCommonResponse(request);
            }
            catch (ServerException ex)
            {
                Console.WriteLine(ex);
                throw;
            }
            catch (ClientException ex)
            {
                Console.WriteLine(ex);
                throw;
            }

            var value = System.Text.Encoding.UTF8.GetString(sendSmsResponse.HttpResponse.Content);
            return value.ToList<dynamic>();
        }
    }

    /// <summary>
    /// 短信结果对象
    /// </summary>
    public class SmsObject
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }
        /// <summary>
        /// 结果代码
        /// </summary>
        public int Code { get; set; }
        /// <summary>
        /// 错误信息
        /// </summary>
        public string ErrMsg { get; set; }
    }
}
